package com.bulunsh.wensen.distributed.oauth.service;

import com.bulunsh.wensen.distributed.oauth.dao.UserDao;
import com.bulunsh.wensen.distributed.oauth.model.UserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MyUserDetailsService implements UserDetailsService {


    @Autowired
    UserDao userDao;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {

        UserDto userDto = userDao.getUserByUsername(s);
        List<String> permissionDtos = userDao.findPermissionByUser(String.valueOf(userDto.getId()));
        //将来链接数据库是在这里链接
        String[] strings = new String[permissionDtos.size()];
        permissionDtos.toArray(strings);
        UserDetails userDetails = User.withUsername(userDto.getUsername()).password(userDto.getPassword()).authorities(strings).build();
        return userDetails;
    }


}
